package justcents.game.sticks;

import justcents.games.andengine.screen.SceneManager;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;
import android.util.Log;

public class MainMenuScreen {
	private static Scene scene;

	//Background Main Menu
	private static BitmapTextureAtlas mBgMainMenuTexture;
	private static TextureRegion mBgMainMenuTextureRegion;
	private static Sprite mBgMainMenuSprite;

	//Efeito do titulo do jogo
	private static BitmapTextureAtlas mBgGameTitleTexture;
	private static TextureRegion mBgGameTitleTextureRegion;
	
	//Titulo do jogo
	private static BitmapTextureAtlas mGameTitleTexture;
	private static TextureRegion mGameTitleTextureRegion;

	//Efeito do nome do jogo offline
	private static BitmapTextureAtlas mBgGameOfflineTitleTexture;
	private static TextureRegion mBgGameOfflineTitleTextureRegion;
	
	//Titulo do jogo offline
	private static BitmapTextureAtlas mGameOfflineTitleTexture;
	private static TextureRegion mGameOfflineTitleTextureRegion;
	private static Sprite gameOfflineTitleSprite;

    /**
     * Load the scene and any assets we need.
     */
    public static void load(Context context)
    {
	    	scene = new Scene();
	        scene.setBackground(new ColorBackground(0,0,0));
	        
	        
	        //Carregando background
            MainMenuScreen.mBgMainMenuTexture = new BitmapTextureAtlas(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
            MainMenuScreen.mBgMainMenuTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(MainMenuScreen.mBgMainMenuTexture, context, "bgMainMenu.png", 0, 0);
            SceneManager.loadTexture(MainMenuScreen.mBgMainMenuTexture);
            
            
            //Carregando Efeito do Titulo do jogo
            MainMenuScreen.mBgGameTitleTexture = new BitmapTextureAtlas(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
            MainMenuScreen.mBgGameTitleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(MainMenuScreen.mBgGameTitleTexture, context, "gameTitleEffect.png", 0, 0);
            SceneManager.loadTexture(MainMenuScreen.mBgGameTitleTexture);
            
            //Carregando Texto do Titulo do jogo
            MainMenuScreen.mGameTitleTexture = new BitmapTextureAtlas(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
            MainMenuScreen.mGameTitleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(MainMenuScreen.mGameTitleTexture, context, "gameTitle.png", 0, 0);
            SceneManager.loadTexture(MainMenuScreen.mGameTitleTexture);
            
            //Carregando Background do Titulo do jogo Offline
            MainMenuScreen.mBgGameOfflineTitleTexture = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
            MainMenuScreen.mBgGameOfflineTitleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(MainMenuScreen.mBgGameOfflineTitleTexture, context, "gameOfflineTitleEffect.png", 0, 0);
            SceneManager.loadTexture(MainMenuScreen.mBgGameOfflineTitleTexture);
            
            //Carregando Texto do Titulo do jogo Offline
            MainMenuScreen.mGameOfflineTitleTexture = new BitmapTextureAtlas(512, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
            MainMenuScreen.mGameOfflineTitleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(MainMenuScreen.mGameOfflineTitleTexture, context, "gameOfflineTitle.png", 0, 0);
            SceneManager.loadTexture(MainMenuScreen.mGameOfflineTitleTexture);
            
            //Carregando Background do Titulo do jogo Offline
            MainMenuScreen.mBgGameOfflineTitleTexture = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
            MainMenuScreen.mBgGameOfflineTitleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(MainMenuScreen.mBgGameOfflineTitleTexture, context, "gameOfflineTitleEffect.png", 0, 0);
            SceneManager.loadTexture(MainMenuScreen.mBgGameOfflineTitleTexture);
            
    }
    
    /**
     * Return the scene for when the scene is called to become active.
     */
    public static Scene run()
    {
    	
    		//Adicionando background do menu principal na cena
    		MainMenuScreen.mBgMainMenuSprite = new Sprite(0, 0, MainMenuScreen.mBgMainMenuTextureRegion);
	    	scene.setBackground(new SpriteBackground(MainMenuScreen.mBgMainMenuSprite));
	    	
	    	//Adicionando efeito do titulo do jogo na cena
            scene.attachChild(new Sprite(0, 0, MainMenuScreen.mBgGameTitleTextureRegion));
            
            //Adicionando texto do titulo do jogo na cena
            scene.attachChild(new Sprite(0, 0, MainMenuScreen.mGameTitleTextureRegion));
            
            //Adicionando efeito do titulo do jogo Offline na cena
            scene.attachChild(new Sprite(0, (Main.CAMERA_HEIGHT / 2) - 40, MainMenuScreen.mBgGameOfflineTitleTextureRegion));

            //Adicionando texto do titulo do jogo Offline na cena
            MainMenuScreen.gameOfflineTitleSprite = new Sprite(0, Main.CAMERA_HEIGHT / 2, MainMenuScreen.mGameOfflineTitleTextureRegion){
            	 @Override
                 public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                                 float pTouchAreaLocalX, float pTouchAreaLocalY) {
                        
                         if(pSceneTouchEvent.isActionDown())
                         {
                        	// load the assets for the second screen
                             GameOfflineMainScreen.load();
                             // switch to the second scene
                             SceneManager.setScene(GameOfflineMainScreen.run());
//                             MainMenuScreen.unload();
                             return true;
                         }
                         return false;
                 }
            };
            scene.registerTouchArea(gameOfflineTitleSprite);
            scene.attachChild(gameOfflineTitleSprite);
            
            //Alimentando o array de objetos da cena
            
            return scene;
    }
   
    /**
     * Unload any assets here - to be called later.
     */
    public static void unload()
    {
           Log.d("UNLOAD", "SIM");
    }
}
